//
//  CheckWorkViewController.m
//  rrqs
//
//  Created by mac book on 2019/4/29.
//

#import "CheckWorkViewController.h"
#import "CheckWorkTableViewCell.h"
#import "UIButton+MCLayout.h"
#import "NSString+Helper.h"
#import "CycleView.h"
#import "NSString+Helper.h"
#import "CheckWorkModel.h"
#import "CGXPickerView.h"

@interface CheckWorkViewController ()
<UITableViewDataSource,
UITableViewDelegate>
{
    CycleView *cycle;
    NSDictionary * dataDict;
    UILabel *timeLabel;
}

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (weak, nonatomic) IBOutlet UIButton *rightBut;
@property (weak, nonatomic) IBOutlet UIButton *rightBottomBut;
@property (weak, nonatomic) IBOutlet UIButton *midBut;
@property (weak, nonatomic) IBOutlet UIButton *midBottomBut;
@property (weak, nonatomic) IBOutlet UIButton *leftBut;

@property (nonatomic, copy)  NSMutableArray * dataArr;


@end

@implementation CheckWorkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mainTableView.delegate  = self;
    _mainTableView.dataSource = self;
    self.view.backgroundColor = kVC_Background_GrayColor;
    [self setRightButton];
    
    cycle = [[CycleView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_leftBut.frame) / 4, CGRectGetMaxY(_leftBut.frame) / 5, CGRectGetWidth(_leftBut.frame)/2, CGRectGetHeight(_leftBut.frame)/2)];
    [_leftBut addSubview:cycle];
    
    UILabel * label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, CGRectGetMaxY(cycle.frame), CGRectGetWidth(_leftBut.frame), 13);
    [_leftBut addSubview:label];
    label.font = [UIFont systemFontOfSize:12];
    label.text = @"今日已在线时长";
    label.textColor = kBlackColor;
    label.textAlignment = NSTextAlignmentCenter;
    
    
    timeLabel = [[UILabel alloc] init];
    timeLabel.frame = CGRectMake(0, 0, CGRectGetWidth(_leftBut.frame), CGRectGetMinY(cycle.frame));
    [_leftBut addSubview:timeLabel];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.textColor = [UIColor redColor];
    
    
    [self queryData:@""];

}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
  
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(void)initViews:(NSDictionary *)data{
    
    NSAttributedString * AttributedString =  [NSString SetFontColorWith:[NSString stringWithFormat:@"%@\n本月在线天数(天)",data[@"daymonth"]] LineSpacing:0 parameter:@[
  @{@"color":kBlackColor,@"Font":KHeavy ,@"size":@12 ,@"rang":@"本月在线天数(天)"},
  @{@"color":kgreen_color,@"Font":KHeavy ,@"size":@18 ,@"rang":[NSString stringWithFormat:@"%@",data[@"daymonth"]]}]];
    [_rightBut setAttributedTitle:AttributedString forState:UIControlStateNormal];
    _rightBut.titleLabel.numberOfLines = 0 ;
    _rightBut.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    NSAttributedString * AS_rightBottom =  [NSString SetFontColorWith:[NSString stringWithFormat:@"%@\n本月不在线天数(天)",data[@"notime"]] LineSpacing:0 parameter:@[
  @{@"color":kBlackColor,@"Font":KHeavy ,@"size":@12 ,@"rang":@"本月不在线天数(天)"},
                                                                                                    
   @{@"color":[UIColor redColor],@"Font":KHeavy ,@"size":@18 ,@"rang":[NSString stringWithFormat:@"%@",data[@"notime"]]}]];
    [_rightBottomBut setAttributedTitle:AS_rightBottom forState:UIControlStateNormal];
    _rightBottomBut.titleLabel.numberOfLines = 0 ;
    _rightBottomBut.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    NSAttributedString * AS_mid =  [NSString SetFontColorWith:[NSString stringWithFormat:@"%@\n本月在线时长(小时)",data[@"hourmonth"]] LineSpacing:0 parameter:@[
    @{@"color":kBlackColor,@"Font":KHeavy ,@"size":@12 ,@"rang":@"本月在线时长(小时)"},
    
  @{@"color":kgreen_color,@"Font":KHeavy ,@"size":@18 ,@"rang":[NSString stringWithFormat:@"%@",data[@"hourmonth"]]}]];
    [_midBut setAttributedTitle:AS_mid forState:UIControlStateNormal];
    _midBut.titleLabel.numberOfLines = 0 ;
    _midBut.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    NSAttributedString * AS_midBottom =  [NSString SetFontColorWith:[NSString stringWithFormat:@"%@\n本月平均在线时长(小时/天)",data[@"rundmonth"] ]LineSpacing:0 parameter:@[
    @{@"color":kBlackColor,@"Font":KHeavy ,@"size":@12 ,@"rang":@"本月平均在线时长(小时/天)"},
  @{@"color":kgreen_color,@"Font":KHeavy ,@"size":@18 ,@"rang":[NSString stringWithFormat:@"%@",data[@"rundmonth"]]}]];
    [_midBottomBut setAttributedTitle:AS_midBottom forState:UIControlStateNormal];
    _midBottomBut.titleLabel.numberOfLines = 0 ;
    _midBottomBut.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    
    cycle.progress = [dataDict[@"daytime"] integerValue] / 8.0;

    NSAttributedString * AS =  [NSString SetFontColorWith:[NSString stringWithFormat:@"%@" ,dataDict[@"daytime"]] LineSpacing:0 parameter:@[@{@"color":kgreen_color,@"Font":KHeavy ,@"size":@18 ,@"rang":[NSString stringWithFormat:@"%@" ,dataDict[@"daytime"]]}]];
    timeLabel.attributedText = AS;
    
    
}
//日历
-(void)setRightButton{
    ///设置返回按钮
    UIImage * rightImg = kCalendarImage;
    UIBarButtonItem * rightBI = [[UIBarButtonItem alloc] initWithImage:rightImg style:UIBarButtonItemStylePlain target:self action:@selector(RightButAction)];
    self.navigationItem.rightBarButtonItem = rightBI;
    
}

-(void)RightButAction{
    
    // 实例化NSDateFormatter
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置日期格式
    [formatter setDateFormat:@"yyyy-MM-dd"];
    // 获取当前日期
    NSDate *currentDate = [NSDate date];
    NSString *currentDateString = [formatter stringFromDate:currentDate];
    
    [CGXPickerView showDatePickerWithTitle:@"请选择日期" DateType:UIDatePickerModeDate DefaultSelValue:nil MinDateStr:nil MaxDateStr:currentDateString IsAutoSelect:NO Manager:nil ResultBlock:^(NSString *selectValue) {
//        [self queryData:[NSString stringWithFormat:@"%ld",[NSString getIntegerwithTime:selectValue]]];
        
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 设置日期格式
        [formatter setDateFormat:@"yyyy-MM-dd"];
        // 设置为UTC时区
        // 这里如果不设置为UTC时区，会把要转换的时间字符串定为当前时区的时间（东八区）转换为UTC时区的时间
        [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        // 要转换的日期字符串
        NSString *dateString = selectValue;
        NSDate *someDay = [formatter dateFromString:dateString];
        // 实例化NSDateFormatter
        // 设置日期格式
        [formatter setDateFormat:@"yyyy-MM"];
        NSString *currentDateString = [formatter stringFromDate:someDay];
        
        [self queryData:[NSString stringWithFormat:@"%@",currentDateString]];

    }];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataArr.count > 0) {
        return  self.dataArr.count;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
#pragma mark 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CheckWorkTableViewCell * cell = (CheckWorkTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"CheckWorkTableViewCell"];
    if (self.dataArr.count > 0) {
        cell.model = self.dataArr[indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    return cell;
}
#pragma mark 点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, kMainScreenWidth- 30, 50)];
    label.text = @"每日在线时长";
    label.font = [UIFont fontWithName:@"AvenirLTStd-Black" size:15];
    [view addSubview:label];
    return view;
}

- (void)queryData:(NSString *)time {
    
    NSDictionary* arg = @{@"ince":@"get_getorder_check",
                          @"time":time,
                      @"emp_id":self.Identity.userInfo.userID};
    NSLog(@"参数： %@",arg);
    __weak typeof(self) weakSelf = self;
    [DBNet PostURL:kHost parameters:arg succeed:^(NSDictionary *data) {
        [WMHelper outPutJsonString:data];
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            [WMHelper outPutJsonString:data];
            weakSelf.dataArr = [CheckWorkModel mj_objectArrayWithKeyValuesArray:data[@"daytimearr"]];
            dataDict = data;
            [weakSelf updatView:data];
            [weakSelf.mainTableView reloadData];
            
        }else{
            [self alertHUD:[data objectForKey:@"fail"]];
        }
        
    } failure:^(NSError *error) {
        [self alertHUD:@"网络异常"];
    }];
}
-(void)updatView:(NSDictionary *)data{
    [self initViews:data];
//    NSAttributedString * AS =  [NSString SetFontColorWith:[NSString stringWithFormat:@"%@小时" ,data[@"daytime"]] LineSpacing:0 parameter:@[
//      @{@"color":kBlackColor,@"Font":KHeavy ,@"size":@12 ,@"rang":@"小时"},
//      @{@"color":kgreen_color,@"Font":KHeavy ,@"size":@18 ,@"rang":[NSString stringWithFormat:@"%@" ,data[@"daytime"]]}]];
//
//    [_leftBut setAttributedTitle:AS forState:UIControlStateNormal];

    
}


@end
